define('p/order/nextday/index', ['./mods/pick-date', './mods/upload', './mods/calendar', '../../../c/base/index', '../../../c/nav/index'], function () {

    $(".fa-circle-o").each(function () {
        if ($(this).parents("a").text() == "次日达交易管理") {
            $(this).parents("a").parents("li").addClass("active");
        }
    });

    function queryNextDayOrderShop(){
        CRM.request({
            "type": 'GET',
            "url": CRM.getOuterUrl('/trade/order/queryNextDayOrderShop'),
            "dataType": "json",
            contentType: "application/x-www-form-urlencoded",
            "data": {},
            "success": function (req) {
                if (req.status) {
                    $("#J_City").html("");
                    $.each(req.entry, function (index, entry) {
                        $("#J_City").append("<option value='" + entry.id + "'>" + entry.city+ "-" +entry.shopName + "</option>")
                    });
                    getLogisticsCompanyForCity();//初始化物流公司
                } else {
                    CRM.showDialog(req.message);
                }
            }
        });
    }

    function getLogisticsCompanyForCity() {
        var cityName = $("#J_City").find("option:selected").text().split("-");
        CRM.request({
            "type": 'POST',
            "url": CRM.getOuterUrl('/trade/order/logisticsCompanyForCity'),
            "dataType": "json",
            contentType: "application/x-www-form-urlencoded",
            "data": {
                cityName: cityName[0],
                isOutLogistics:0
            },
            "success": function (req) {
                if (req.status) {
                    $("#J_logisticsCompany").html("");
                    $.each(req.entry, function (index, entry) {
                        $("#J_logisticsCompany").append("<option value='" + entry + "'>" + entry + "</option>")
                    });
                } else {
                    CRM.showDialog(req.message);
                }
            }
        });
    }


    function queryLogisticsCompanyList() {
        CRM.request({
            "type": 'POST',
            "url": CRM.getOuterUrl('/trade/order/queryLogisticsCompanyList'),
            "dataType": "json",
            contentType: "application/x-www-form-urlencoded",
            "data": {},
            "success": function (req) {
                if (req.status) {
                    $("#U_logisticsCompany").html("");
                    $.each(req.entry, function (index, entry) {
                        $("#U_logisticsCompany").append("<option value='" + entry + "'>" + entry + "</option>")
                    });
                    upload();
                } else {
                    CRM.showDialog(req.message);
                }
            }
        });
    }

    $('#J_City').on('change', function (e) {
        getLogisticsCompanyForCity();
    });
    $('#U_logisticsCompany').on('change', function (e) {
        upload();
    });
    queryNextDayOrderShop();
    queryLogisticsCompanyList();//上传物流信息 物流公司：
    $('#Q_DateStart').calendar({format: 'yyyy-MM-dd'});
    $('#Q_DateEnd').calendar({format: 'yyyy-MM-dd HH:mm:ss'});
    $('#J_DateStart').calendar({format: 'yyyy-MM-dd'});
    $('#J_DateEnd').calendar({format: 'yyyy-MM-dd HH:mm:ss'});

    (function () {
        var tpl = '<div class="dialog-content J_DialogContent" style="top:{top}px;left:{left}px;">' +
            '<em class="dialog-close dialog-iconfont J_DialogClose">&#xe600;</em>' +
            '<p class="dialog-txt">{txt}</p>' +
            '<p class="dialog-btn-content">' +
            '<span class="dialog-btn J_DialogBtn">确定</span>' +
            '</p>' +
            '</div>';
        var loadingTpl = '<div class="dialog-content J_DialogContent J_DialogLoading" style="top:{top}px;left:{left}px;">' +
                // '<em class="dialog-close dialog-iconfont J_DialogClose">&#xe600;</em>'+
            '<p class="dialog-txt"><img src="http://imgsize.52shangou.com/img/n/12/22/1450763881027_2190.gif"></p>' +
                // '<p class="dialog-btn-content">'+
                // '<span class="dialog-btn J_DialogBtn">确定</span>'+
                // '</p>'+
            '</div>';

        var scrollHeight = document.documentElement.scrollHeight;
        var scrlllWidth = document.documentElement.scrollWidth;


        function showMask() {
            $(document.body).append('<div class="mask-dialog J_MaskDialog" style="height:' + scrollHeight + 'px;"></div>');
        }

        function sub(str, data) {
            return str
                .replace(/{(.*?)}/igm, function ($, $1) {
                    return data[$1] ? data[$1] : $;
                });
        }

        function bindEvent(el, callback) {
            el.find('.J_DialogClose').on('click', function () {
                el.remove();
                $('.J_MaskDialog').remove();
            });
            el.find('.J_DialogBtn').on('click', function () {
                el.remove();
                $('.J_MaskDialog').remove();
                callback && callback();

            });
        }

        function showDialog(txt, callback) {
            var top = window.scrollY + ($(window).height() / 2 - 81);
            var left = ($(window).width() / 2 - 201);
            var el = $(sub(tpl, {
                left: left,
                top: top,
                txt: txt
            }));
            showMask();
            el.appendTo($(document.body));
            bindEvent(el, callback);
        }

        function showLoading() {
            var top = window.scrollY + ($(window).height() / 2 - 81);
            var left = ($(window).width() / 2 - 201);
            var el = $(sub(loadingTpl, {
                left: left,
                top: top
            }));
            showMask();
            el.appendTo($(document.body));
            // bindEvent(el);
        }


        window.showDialog = showDialog;
        window.showLoading = showLoading;
        window.hideLoading = function () {
            $('.J_MaskDialog,.J_DialogLoading').remove();
        }
    })();


    $('#J_Download').bind('click', function (e) {
        var start = $('#Q_DateStart').val();
        var end = $('#Q_DateEnd').val();
        if (!start) {
            CRM.showDialog('请选择开始日期');
            return;
        }else if (!end) {
            CRM.showDialog('请选择结束日期');
            return;
        }
        location.href = CRM.getOuterUrl('/trade/nextDayArrive/downloadOrderForLogistics?' +
                //'startDate='+start +
            'startDate=' + start +
            '&endDate=' + end +
            '&city=' + $('#J_City').val() +
            '&shopId=' + $('#J_City').val() +
            '&logisticsCompany=' + $('#J_logisticsCompany').val() +
            '&itemId=' + $("#J_ItemId").val());
    });

    $('#J_query').bind('click', function (e) {
        var start = $('#J_DateStart').val();
        var end = $('#J_DateEnd').val();
        if (!start) {
            CRM.showDialog('请选择开始日期');
            return;
        }else if (!end) {
            CRM.showDialog('请选择结束日期');
            return;
        }
        $('#J_queryList').html('');
        $.ajax({
            url: CRM.getOuterUrl('/trade/nextDayArrive/queryForDownload?endDate=' + end + '&startDate=' + start),
            type: 'get',
            xhrFields: {
                withCredentials: true
            },
            success: function (data) {
                if (!data.status) {
                    window.showDialog(data.message);
                    return;
                }
                var str = '<li class="title">\
						<span class="city">城市</span>\
						<span class="itemId">商品id</span>\
						<span class="name">商品名</span>\
						<span class="count">待发货商品量</span>\
					</li>';

                data.entry.forEach(function (item, i) {
                    str += '<li>\
									<span class="city">' + item.city + '</span>\
									<span class="itemId">' + item.itemId + '</span>\
									<span class="name">' + item.itemName + '</span>\
									<span class="count">' + item.count + '</span>\
								</li>';
                });
                $('#J_queryList').html(str);
            }
        });
    });

    function upload() {
        new Uploader({
            trigger: '#J_UploadLogistics',
            name: 'file',
            action: CRM.getOuterUrl('/trade/nextDayArrive/uploadDelivery'),
            //accept: 'image/*',
            data: {
                // // date:elDataExport.val()
                // startDate: startDate,
                logisticsCompany: $("#U_logisticsCompany").val()
            },
            multiple: false,
            error: function (file) {
                alert('上传失败');
                upload();
                // console.log(file);
                // elDataExport.trigger('blur');
            },
            success: function (data) {
                alert(data.message);
                upload();
                // $('.J_ExportBillDate').trigger('blur');

            }
        });
    }
});
